//处理模型
const {
    successModel,
    failModel
} = require('../model/responseModel')

const {
    getGood,
    getCartInfo,
    searchGoods
} = require('../controller/good')


//获取商品相关的路由
const handleGoodRoute = (req, res) => {
    //定义处理路由的逻辑
    const method = req.method;
    switch (method) {
        case 'GET': {
            if (req.path === '/good') {
                return getGood(req.query).then(data => {
                    if (data) {
                        return new successModel(data)
                    } else {
                        return new failModel('获取商品数据失败')
                    }
                })
            }
            else if(req.path === '/good/cartInfo'){
                return getCartInfo(req.query).then(data => {
                    if (data) {
                        return new successModel(data)
                    } else {
                        return new failModel('获取购物车商品数据失败')
                    }
                })
            } else if(req.path === '/good/search'){
                return searchGoods(req.query).then(data => {
                    if (data) {
                        return new successModel(data)
                    } else {
                        return new failModel('获取商品数据失败')
                    }
                })
            }
            
            
            else{
                return false;
            }
        };
    default: {
        return false;
    }
    }

}

module.exports = handleGoodRoute